package com.ejuornal.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Page;
import com.ejuornal.bean.SysMsg;
import com.ejuornal.dao.SysMsgDao;
/**
 * 项目名称：ejuornal    
 * 类名称：SysMsgDaoImpl    
 * 类描述： 站内信系统通知持久层实现类   
 * 创建人：陈晨    
 * 创建时间：Feb 27, 2013 8:06:08 AM    
 * 修改人：陈晨    
 * 修改时间：Feb 27, 2013 8:06:08 AM    
 * 修改备注：    
 * @version     
 */

@Component("sysMsgDao")
public class SysMsgDaoImpl extends DaoSupport<SysMsg> implements SysMsgDao{

    @Override
    public void add(String sql, Object... objs) {
        super.add(sql, objs);
    }

    @Override
    public void batchDelete(String sql, int... ids) {
        super.batchDelete(sql, ids);
    }

    @Override
    public Page<SysMsg> find(int current_record, String url) {
        return super.find(current_record, url);
    }

    @Override
    public Page<SysMsg> find(int current_record, String url, String hsql, Object[] objs) {
        return super.find(current_record, url, hsql, objs);
    }
    /**
     * 通过编号查找详情
     */
    public SysMsg find(int smId) {
         JdbcTemplate jt = super.getJdbcTemplate();
         SysMsg s = jt.queryForObject("select * from t_sys_msg t where t.smId=?", 
                         new Object[]{smId}, 
                         new RowMapper<SysMsg>() {

                            public SysMsg mapRow(ResultSet rs, int rowNum) throws SQLException {
                                SysMsg sm = new SysMsg();
                                sm.setSmId(rs.getInt("smId"));
                                sm.setSmTitle(rs.getString("smTitle"));
                                sm.setSmContent(rs.getString("smContent"));
                                sm.setSmSender(rs.getString("smSender"));
                                sm.setSmType(rs.getString("smType"));
                                sm.setSmDate(rs.getDate("smDate"));
                                return sm;
                            }
                         }
         );
         return s;
    }
    
    
    @Override
	public Page<SysMsg> find(int current_record, String url, String sql_1,
			String sql_2, Object[] objs) {
		// TODO Auto-generated method stub
		return super.find(current_record, url, sql_1, sql_2, objs);
	}

	@Override
    public Page<SysMsg> findByStatus(String status, int user_id, int current_record, String url) {
        Page<SysMsg> page = new Page<SysMsg>();
        String sql = "select count(*) from t_sys_msg";
        String sql_2 = "select * from t_sys_msg limit ?, ?";
        @SuppressWarnings("deprecation")
		int totalRecords = this.getJdbcTemplate().queryForInt(sql);
        List<SysMsg> list =this.getJdbcTemplate().query(sql_2, 
                    new Object[]{ current_record, 10},
                    new RowMapper<SysMsg>() {
            
                        public SysMsg mapRow(ResultSet rs, int rowNum) throws SQLException {
                            SysMsg sm = new SysMsg();
                            sm.setSmId(rs.getInt("smId"));
                            sm.setSmTitle(rs.getString("smTitle"));
                            sm.setSmContent(rs.getString("smContent"));
                            sm.setSmDate(rs.getDate("smDate"));
                            sm.setSmType(rs.getString("smType"));
                            return sm;
                        }
                     }
        );
        page.setUrl(url);
        page.setTotalRecords(totalRecords);
        page.setTotalPage(totalRecords);
        page.setRecords(list);
        return page;
    }

    @Override
    public boolean findNew(int user_id) {
        String sql= "SELECT COUNT(*) FROM t_sys_msg_status WHERE userId=? and isRead=0";
        int num = super.getJdbcTemplate().queryForInt(sql, new Object[]{user_id});
        if(num > 0 ) {
            return true;
        }
        return false;
    }
    
}
